/* eslint valid-jsdoc: "off" */

'use strict';
const path = require('path');

const baseType = [{
  type: 'PC',
  hosts: [ 'localhost:8080', '127.0.0.1:8080', '10.10.70.44:8080' ],
}, {
  type: 'mobile',
  hosts: [ 'localhost:8000', '127.0.0.1:8000', '10.10.70.44:8000' ],
}];

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1575812978932_7706';

  // add your middleware config here
  config.middleware = [];

  // config.httpLog = {
  //   type: 'all',
  // };

  config.getDeviceType = baseType;

  config.allowHosts = [ ...baseType[0].hosts, ...baseType[1].hosts ];

  config.interfaceLimit = {
    maxCount: 100,
    time: 3000,
  };

  config.security = {
    csrf: {
      enable: false,
    },
  };

  exports.multipart = {
    fileSize: '50mb',
    mode: 'stream',
  };

  config.view = {
    mapping: {
      '.html': 'ejs',
    },
    root: [
      path.join(appInfo.baseDir, 'app/html'),
      path.join(appInfo.baseDir, 'app/view'),
    ].join(','),
  };

  config.ejs = {
    delimiter: '%',
  };

  config.session = {
    key: 'BLUE_SESS',
    httpOnly: true,
    maxAge: 1000 * 3600 * 12,
    renew: true,
  };

  config.auth = {
    exclude: [ 'POST /api/user/login', 'POST /api/user' ],
  };

  config.sequelize = {
    dialect: 'mysql',
    host: '127.0.0.1',
    port: '3306',
    user: 'root',
    password: '',
    database: 'trip',
    define: {
      timestamps: true, // 添加create,update,delete时间戳
      paranoid: true, // 添加软删除
      freezeTableName: true,
    },
  };

  config.jwt = {
    secret: 'blue',
  };

  config.redis = {
    client: {
      port: 6379,
      host: '127.0.0.1',
      db: 0,
      password: '',
    },
  };

  // add your user config here
  const userConfig = {
    salt: 'blue',
    redisExpire: 60 * 60 * 24,
    isValidate: false,
  };

  config.validatePlus = {
    convert: true,
    // 这里是设置参数不正确的回调函数 我们需要返回给客户端相应地错误信息
    resolveError(ctx, errors) {
      if (errors.length) {
        ctx.body = {
          code: 500,
          message: errors[0].message,
        };
      }
    },
  };

  config.onerror = {
    all(err, ctx) {
      ctx.body = {
        status: 500,
        message: '服务器开小差了',
      };
    },
  };

  return {
    ...config,
    ...userConfig,
  };
};
